package org.testmyapp.listeners;

import java.util.ArrayList;
import java.util.List;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.testmyapp.entities.AccountingMedical;
import org.testmyapp.entities.Bill;
import org.testmyapp.entities.BillPosition;
import org.testmyapp.entities.RefundPart;
import org.testmyapp.global.Variable;
import org.testmyapp.persistence.BusinessObjectMarker;
import org.testmyapp.persistence.BusinessObjectPersistenceProvider;
import org.testmyapp.session.SessionForUser;

@Named(value = "endAccountingMedical")
@ApplicationScoped
public class AccountingMedicalEndListener implements ExecutionListener {

	@Inject
	private SessionForUser session;

	@Inject
	private BusinessObjectPersistenceProvider bopp;
	
	public static final String successCriteriaEventName = "successfulEnd"; 

	public void notify(DelegateExecution execution) throws Exception {
		
		// Wird im Fall von "failureEnd" oder aehnlichem Betreten (kein erfolgreicher Prozessabschluss)
		if (!execution.getEventName().equals("successfulEnd")) return;
		
		// For GlassFish-Log
		System.out.println("Persisting Process state for " + execution.getProcessInstanceId() + " in successful end Accounting Medical");
		@SuppressWarnings("unchecked")
		List<Bill> bills = (List<Bill>) execution.getVariable(Variable.BILLLIST
				.getName());
		@SuppressWarnings("unchecked")
		List<BillPosition> billPositions = (List<BillPosition>) execution
				.getVariable(Variable.POSITIONLIST.getName());
		@SuppressWarnings("unchecked")
		List<RefundPart> refundParts = (List<RefundPart>) execution
				.getVariable(Variable.REFUNDPARTLIST.getName());
		AccountingMedical accMedical = (AccountingMedical) execution
				.getVariable(Variable.ACCMEDICAL.getName());

		
		List<BusinessObjectMarker> boList = new ArrayList<BusinessObjectMarker>();
		boList.addAll(refundParts);
		bopp.persistList(boList);
		
		// TODO MAp with retentions
	}

}
